<?php

header("Content-type: text/html; charset=utf-8");
$out .= sprintf("<link type='text/css' href='<?php echo \$TEMA;?>' rel='stylesheet' />\n");
if(($_REQUEST['Menu']) == "Novo")
{
     $out .= sprintf("<link rel=\"stylesheet\" href=\"css/menu.css\" type=\"text/css\" />\n");
     $out .= sprintf("<script type=\"text/javascript\" src=\"js/menu.js\"></script>\n");
}
else if(($_REQUEST['Menu']) == "Antigo")
{
     $out .= sprintf("<link rel='stylesheet' type='text/css' href='css/estilos.css' />\n");
}
else if(($_REQUEST['Menu']) == "Menu1")
{
     $out .= sprintf("<link rel=\"stylesheet\" href=\"css/menu1.css\" type=\"text/css\" />\n");
}
else if(($_REQUEST['Menu']) == "Menu2")
{
     $out .= sprintf("<script type='text/javascript' src='js/fg.menu.js'></script>\n");
     $out .= sprintf("<link type='text/css' href='css/fg.menu.css' media='screen' rel='stylesheet' />\n");
}
else if(($_REQUEST['Menu']) == "Maujor")
{
     $out .= sprintf("<script type='text/javascript' src='js/maujor.js'></script>\n");
     $out .= sprintf("<link type='text/css' href='css/maujor.css' media='screen' rel='stylesheet' />\n");
}
$sql = sprintf("SELECT campo,tipo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha", $TABLE, $SCRIPT);
$result = mysql_query($sql, $dbctl);
if($result == 0)
{
     printf("<h1>(PrepMenu-5b)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($result) > 0)
     {
          while($myrow = mysql_fetch_row($result))
          {
               if($myrow[1] == "mediumblob")
               {
                    if(strtoupper($myrow[0]) == 'DOCX')
                    {
                         $out .= sprintf("<script src=\"js/jquery.ui.widget.js\" type=\"text/javascript\"></script>\n");
                         $out .= sprintf("<script src=\"js/jquery.iframe-transport.js\" type=\"text/javascript\"></script>\n");
                         $out .= sprintf("<script src=\"js/jquery.fileupload.js\" type=\"text/javascript\"></script>\n");
                    }
               }
          }
     }
}

if(isset($_REQUEST['AGENDA1']))
{
     $out .= sprintf("<link href=\"agenda/css/dailog.css\" rel=\"stylesheet\" type=\"text/css\" />\n");
     $out .= sprintf("<link href=\"agenda/css/calendar.css\" rel=\"stylesheet\" type=\"text/css\" />\n");
     $out .= sprintf("<link href=\"agenda/css/dp.css\" rel=\"stylesheet\" type=\"text/css\" />\n");
     $out .= sprintf("<link href=\"agenda/css/alert.css\" rel=\"stylesheet\" type=\"text/css\" />\n");
     $out .= sprintf("<link href=\"agenda/css/main.css\" rel=\"stylesheet\" type=\"text/css\" />\n");

     $out .= sprintf("<script src=\"agenda/src/jquery.js\" type=\"text/javascript\"></script>\n");
     $out .= sprintf("<script src=\"agenda/src/Plugins/Common.js\" type=\"text/javascript\"></script>\n");
     $out .= sprintf("<script src=\"agenda/src/Plugins/jquery.alert.js\" type=\"text/javascript\"></script>\n");
     $out .= sprintf("<script src=\"agenda/src/Plugins/jquery.ifrmdailog.js\" defer=\"defer\" type=\"text/javascript\"></script>\n");
     $out .= sprintf("<script src=\"agenda/src/Plugins/jquery.calendar.js\" type=\"text/javascript\"></script>\n");
}

$out .= sprintf("<link rel=\"stylesheet\" href=\"css/comum.css\" type=\"text/css\" />\n");
$out .= sprintf("<link rel=\"stylesheet\" href=\"css/table.css\" type=\"text/css\" />\n");
$out .= sprintf("<script type=\"text/javascript\" src=\"js/alert.js\"></script>\n");
$out .= sprintf("<script type=\"text/javascript\" src=\"js/jquery-1.6.4.min.js\"></script>\n");
$out .= sprintf("<script type=\"text/javascript\" src=\"js/jquery-ui-1.8.16.custom.min.js\"></script>\n");
$out .= sprintf("<script type=\"text/javascript\" src=\"js/jquery.blockUI.js\"></script>\n");
$out .= sprintf("<script type=\"text/javascript\" src=\"js/jquery.maskedinput-1.3.min.js\"></script>\n");
$out .= sprintf("<script type=\"text/javascript\" src=\"js/jquery.ui.datepicker-pt-BR.js\"></script>\n");
if($_REQUEST['Tabela'] == 'Nova')
{
     $out .= sprintf("<script type=\"text/javascript\" src=\"js/jquery.dataTables.min.js\"></script>\n");
}
$out .= sprintf("<script type=\"text/javascript\" src=\"js/toolbar.js\"></script>\n");

$lnk = '';
$aba_lnk = '';
Check_Link($db,$TABLE, $lnk, $aba_lnk);
if($lnk != '' || $_REQUEST['Tabela'] == 'Jqgrid')
{
     $out .= sprintf("\n<link rel=\"stylesheet\" type=\"text/css\" href=\"jqgrid/css/ui.jqgrid.css\" />\n");
     $out .= sprintf("<link rel=\"stylesheet\" type=\"text/css\" href=\"jqgrid/plugins/ui.multiselect.css\" />\n");
     $out .= sprintf("<script type=\"text/javascript\" src=\"jqgrid/plugins/ui.multiselect.js\"></script>\n");
     $out .= sprintf("<script type=\"text/javascript\" src=\"jqgrid/js/i18n/grid.locale-pt-br.js\"></script>\n");
     $out .= sprintf("<script type=\"text/javascript\" src=\"jqgrid/js/jquery.jqGrid.min.js\"></script>\n");
     $out .= sprintf("<script type=\"text/javascript\">\n");
     $out .= sprintf("     $.jgrid.no_legacy_api = true;\n");
     $out .= sprintf("     $.jgrid.useJSON = true;\n");
     $out .= sprintf("</script>\n");
     $out .= sprintf("<style type=\"text/css\">\n");
     $out .= sprintf("     .rowColorGREEN {background-color: #a5d9de;}\n");
     $out .= sprintf("     .ui-jqgrid tr.jqgrow td { white-space:nowrap;}\n");
     $out .= sprintf("</style>\n");
     $out .= Mount_Link_Js($db, $TABLE);
}

if(($_REQUEST['Menu']) == "Potato")
{
     $out .= sprintf("<script type='text/javascript' src='js/jquery.ui.potato.menu.js'></script>\n");
     $out .= sprintf("<link type='text/css' href='css/jquery.ui.potato.menu.css' media='screen' rel='stylesheet' />\n");
     $out .= sprintf("<script type='text/javascript'>\n");
     $out .= sprintf("  (function($) {\n");
     $out .= sprintf("    $(document).ready(function(){\n");
     $out .= sprintf("    $('#potato').ptMenu();\n");
     $out .= sprintf("    });\n");
     $out .= sprintf("  })(jQuery);\n");
     $out .= sprintf("</script>\n");
}
$out .= SetDirAba($db,$TABLE, $SCRIPT);
if(isset($_REQUEST['Aviso']))
{
     $out .= AvisoJS();
}
$chave = "";
$sql = sprintf("SELECT campo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > 0 ORDER BY aba_order,aba,Linha LIMIT 1", $TABLE, $SCRIPT);
$result = mysql_query($sql, $dbctl);
if($result == 0)
{
     printf("<h1>(PrepMenu-1)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($result) > 0)
     {
          while($myrow = mysql_fetch_row($result))
          {
               $chave = $myrow[0];
          }
     }
}
$pesq = "";
if(substr(strtolower($chave), 0, 2) == "id")
{
     $pesq .= sprintf("\$('#PESQUISA').val($('#%s option:selected').text());\n", strtoupper($chave));
}
else
{
     $pesq .= sprintf("\$('#PESQUISA').val($('#%s').val());\n", strtoupper($chave));
}

$js = "";
$sql = sprintf("SELECT campo FROM Page_mount WHERE Tabela='%s' AND script='%s'", $TABLE, $SCRIPT);
$result = mysql_query($sql, $dbctl);
if($result == 0)
{
     printf("<h1>(PrepMenu-2)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($result) > 0)
     {
          while($myrow = mysql_fetch_row($result))
          {
               if(substr(strtolower($myrow[0]), 0, 2) == "id")
               {
                    if(strstr($myrow[0], "_id") != "")
                    {
                         $js .= sprintf("var %s = 0;\n", $myrow[0]);
                    }
               }
          }
     }
}
$js .= sprintf("var current = 0;\n");
$js .= sprintf("var idsessao = 0;\n");
$js .= sprintf("var popupid = 0;\n");
$js .= sprintf("var old = 0;\n");
$js .= sprintf("var edit = 0;\n");
$js .= sprintf("var last_lin = 0;\n");
$js .= sprintf("var oTable;\n");

$js .= sprintf("\$(document).ready(function() {\n");
$js .= sprintf("     $('#dialog-info').dialog({\n");
$js .= sprintf("          autoOpen: false,\n");
$js .= sprintf("          resizable: false,\n");
$js .= sprintf("          width: 400,\n");
$js .= sprintf("          height:200,\n");
$js .= sprintf("          modal: true,\n");
$js .= sprintf("          buttons: {\n");
$js .= sprintf("               'OK': function() {\n");
$js .= sprintf("               $( this ).dialog('close');\n");
$js .= sprintf("               }\n");
$js .= sprintf("          },\n");
$js .= sprintf("          close: function() {\n");
$js .= sprintf("          }\n");
$js .= sprintf("     });\n");
$js .= sprintf("     GET('last');\n");
$js .= sprintf("     GETLIST('last');\n");
$js .= sprintf("     Disable();\n");
if($_REQUEST['Tabela'] != 'Jqgrid')
{
     $js .= sprintf("     oTable = $('#TABLE').dataTable( {\n");
     $js .= sprintf("          'bProcessing': true,\n");
     $js .= sprintf("          'bServerSide': true,\n");
     $js .= sprintf("          'sAjaxSource': 'my_ajax/Cad%s_ajax.php',\n", $SCRIPT);
     $js .= sprintf("          'sScrollY': '350px',\n");
     $js .= sprintf("          'sScrollX': '100%%',\n");
     $js .= sprintf("          'bFilter': true,\n");
     $js .= sprintf("          'sPaginationType': 'full_numbers',\n");
     $js .= sprintf("          'oLanguage': {\n");
     $js .= sprintf("               'sProcessing':   'Processando...',\n");
     $js .= sprintf("               'sLengthMenu':   'Mostrar _MENU_ registros',\n");
     $js .= sprintf("               'sZeroRecords':  'Não foram encontrados resultados',\n");
     $js .= sprintf("               'sInfo':         'Mostrando de _START_ até _END_ de _TOTAL_ registros',\n");
     $js .= sprintf("               'sInfoEmpty':    'Mostrando de 0 até 0 de 0 registros',\n");
     $js .= sprintf("               'sInfoFiltered': '(Total de _MAX_ registros)',\n");
     $js .= sprintf("               'sInfoPostFix':  '',\n");
     $js .= sprintf("               'sSearch':       'Buscar:',\n");
     $js .= sprintf("               'sUrl':          '',\n");
     $js .= sprintf("               'oPaginate': {\n");
     $js .= sprintf("                    'sFirst':    'Primeiro',\n");
     $js .= sprintf("                    'sPrevious': 'Anterior',\n");
     $js .= sprintf("                    'sNext':     'Seguinte',\n");
     $js .= sprintf("                    'sLast':     'Último'\n");
     $js .= sprintf("               }\n");
     $js .= sprintf("          }\n");
     $js .= sprintf("     } );\n\n");
}
else
{
     $names = array();
     prepColModel($dbctl, $TABLE, $SCRIPT, $names);
     $js .= sprintf("     jQuery(\"#list_dialog\").jqGrid({\n");
     $js .= sprintf("     url:'my_ajax/Cad%s_ajax.php',\n", ucfirst($SCRIPT));
     $js .= sprintf("     datatype: \"json\",\n");
     $sql = sprintf("SELECT label,campo,tipo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Lista > '0' ORDER BY aba_order,aba,Lista", $TABLE, $SCRIPT);
     $result = mysql_query($sql, $dbctl);
     if($result == 0)
     {
          printf("<h1>Falha: %s</h1>\n", mysql_error($dbctl));
          printf("<h3>%s</h3>\n", $sql);
          exit;
     }
     else
     {
          if(mysql_num_rows($result) > 0)
          {
               $n = 0;
               $first = '';
               $list = "     colNames:[";
               $model = "     colModel:[";
               while($myrow = mysql_fetch_row($result))
               {
                    if($myrow[2] == "mediumblob")
                    {
                         continue;
                    }
                    if($n == 0)
                    {
                         if(substr(strtolower($myrow[1]), 0, 2) == "id")
                         {
                              $sel = ucfirst(substr($myrow[1], 2, strlen($myrow[1]) - 2));
                              if($sel != ucfirst($table))
                              {
                                   $cp = GetCampo1($db, $myrow[1]);
                                   $first = strtolower($sel) . "_" . strtolower($cp);
                              }
                              else
                              {
                                   $first = strtolower($sel) . "_" . strtolower($myrow[1]);
                              }
                         }
                         else
                         {
                              $first = strtolower($TABLE) . "_" . strtolower($myrow[1]);
                         }
                         if(substr(strtolower($myrow[0]), 0, 2) == "id")
                         {
                              $list .= "'".ucfirst(substr($myrow[0], 2, strlen($myrow[0]) - 2))."'";
                         }
                         else
                         {
                              $list .= "'$myrow[0]'";
                         }
                         $model .= "     {\n";
                         //$model .= sprintf("          name:'%s',\n",strtolower($myrow[1]));
                         //$model .= sprintf("          index:'%s',\n",strtolower($myrow[1]));
                         $model .= sprintf("          name:'%s',\n",$names[$n]);
                         $model .= sprintf("          index:'%s',\n",$names[$n]);
                         $model .= "          align:'left', \n";
                         $model .= "          sortable:true,\n"; 
                         $model .= "          search:true,\n";
                         $model .= "          frozen:true\n";
                         $model .= "     }\n";
                    }
                    else
                    {
                         if(substr(strtolower($myrow[0]), 0, 2) == "id")
                         {
                              $list .= ",'".ucfirst(substr($myrow[0], 2, strlen($myrow[0]) - 2))."'";
                         }
                         else
                         {
                              $list .= ",'$myrow[0]'";
                         }
                         $model .= "     ,{\n";
                         //$model .= sprintf("          name:'%s',\n",strtolower($myrow[1]));
                         //$model .= sprintf("          index:'%s',\n",strtolower($myrow[1]));
                         $model .= sprintf("          name:'%s',\n",$names[$n]);
                         $model .= sprintf("          index:'%s',\n",$names[$n]);
                         $model .= "          align:'left', \n";
                         $model .= "          sortable:true,\n";
                         $model .= "          search:true,\n";
                         $model .= "          frozen:false\n";
                         $model .= "     }\n";
                    }
                    $n++;
               }
               $list .= "],\n";
               $js .= $list;
               $js .= $model;
               
               $js .= "     ],\n";
               $js .= "     altRows: true,\n";
               $js .= "     autowidth: false,\n";
               $js .= "     rowNum:50,\n";
               $js .= "     rowList:[5,10,15,30,50,100,200,500,1000],\n";
               $js .= "     pager: '#pager_list_dialog',\n";
               $js .= "     viewrecords: true,\n";
               $js .= "     scroll: false,\n";
               $js .= "     scrollrows: false,\n";
               $js .= sprintf("     sortname: '%s',\n",$first);
               $js .= "     sortorder: 'asc',\n";
               $js .= "     width: 760,\n";
               $js .= "     shrinkToFit: false,\n";
               $js .= "     height: 360,\n";
               $js .= "     gridview: true,\n";
               $js .= sprintf("     caption:'Cadastro de %s',\n",ucfirst($SCRIPT));
               $js .= "     loadComplete: function () {\n";
               $js .= "          var cl;\n";
               $js .= "          var trElement;\n";
               $js .= "          var rowIDs = jQuery('#list_dialog').getDataIDs();\n";
               $js .= "          for (var i=0;i<rowIDs.length;i=i+1){\n";
               $js .= "               cl = rowIDs[i];\n";
               $js .= "               trElement = jQuery('\"#'+cl+'\"',jQuery('#list_dialog'));\n";
               $js .= "               trElement.removeClass('ui-widget-content');\n";
               $js .= "               trElement.addClass('rowColorGREEN');\n";
               $js .= "               }\n";
               $js .= "          }\n";
               $js .= "     });\n";
               $js .= "     jQuery('#list_dialog')\n";
               $js .= "     .navGrid('#pager_list_dialog',{\n";
               $js .= "          edit:false,\n";
               $js .= "          add:false,\n";
               $js .= "          del:false,\n";
               $js .= "          search:true,\n";
               $js .= "          excel:true\n";
               $js .= "     })\n";
               $js .= "     .navButtonAdd('#pager_list_dialog',{\n";
               $js .= "          title:'Exportar para Excel',\n";
               $js .= "          caption:'Excel ',\n";
               $js .= "          buttonicon:'ui-icon-calculator',\n";
               $js .= "          onClickButton: function(){\n";
               $js .= "               exportExcel();\n";
               $js .= "          }\n";
               $js .= "     })\n";
               $js .= "     .navButtonAdd('#pager_list_dialog',{\n";
               $js .= "          title:'Selecionar Colunas',\n";
               $js .= "          caption:'Selecionar ',\n";
               $js .= "          buttonicon:'ui-icon-calculator',\n";
               $js .= "          onClickButton: function(){\n";
               $js .= "               jQuery('#list_dialog').jqGrid('columnChooser');\n";
               $js .= "          },\n";
               $js .= "          position:'last'\n";
               $js .= "     });\n";

               $js .= "     jQuery('#list_dialog').jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});\n";
               $js .= "     jQuery('#list_dialog').trigger('reloadGrid');\n";
               //$js .= "     jQuery('#list_dialog').jqGrid('setFrozenColumns');\n";
               
          }
     }
}
$sql = sprintf("SELECT campo,tipo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha", $TABLE, $SCRIPT);
$result = mysql_query($sql, $dbctl);
if($result == 0)
{
     printf("<h1>(PrepMenu-3)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($result) > 0)
     {
          while($myrow = mysql_fetch_row($result))
          {
               if($myrow[1] == "mediumblob")
               {
                    if(strtoupper($myrow[0]) != 'DOCX')
                    {
                         $js .= sprintf("\$('#B%s').click(function() {\n", strtoupper($myrow[0]));
                         if(strtoupper($myrow[0]) == 'LOGO')
                         {
                              $js .= sprintf("     var value = \"ImageUp/logoup.php?IMAGEM=\" + current + \"&DIR=%s_%s\";\n", strtolower($TABLE), strtolower($myrow[0]));
                         }
                         else if(strtoupper($myrow[0]) == 'FOTO')
                         {
                              $js .= sprintf("     var value = \"ImageUp/imageup.php?IMAGEM=\" + current + \"&DIR=%s_%s\";\n", strtolower($TABLE), strtolower($myrow[0]));
                         }
                         else if(strtoupper($myrow[0]) == 'DOC')
                         {
                              $js .= sprintf("     var value = \"doc_up.php?DOC=\" + current + \"&DIR=%s_%s\";\n", strtolower($TABLE), strtolower($myrow[0]));
                         }
                         $js .= sprintf("     window.open(value,'%s','width=700, height=950, scrollbars=yes, location=no');\n", ucfirst($myrow[0]));
                         $js .= sprintf("});\n");
                    }
               }
          }
     }
}
$js .= sprintf("});\n\n");

$js .= "function exportExcel()\n";
$js .= "{\n";
$js .= "     var mya=new Array();\n";
$js .= "     mya=$('#list_dialog').getDataIDs();\n";
$js .= "     var data=$('#list_dialog').getRowData(mya[0]);\n";
$js .= "     var colNames=new Array();\n";
$js .= "     var ii=0;\n";
$js .= "     for (var i in data){colNames[ii++]=i;}\n";
$js .= "     var html=\"\";\n";
$js .= "     var columnNames = $('#list_dialog').jqGrid('getGridParam','colNames');\n";
$js .= "     for(i=0;i<columnNames.length;i++){\n";
$js .= "          html=html+\"\\\"\"+columnNames[i]+\"\\\"\"+\";\";\n";
$js .= "     }\n";
$js .= "     html=html+\"\\n\";\n";
$js .= "     for(i=0;i<mya.length;i++){\n";
$js .= "          data=$('#list_dialog').getRowData(mya[i]);\n";
$js .= "          for(j=0;j<colNames.length;j++){\n";
$js .= "               var strold=data[colNames[j]];\n";
$js .= "               var strnew=strold.replace(/\"/g,\"'\");\n";
$js .= "               var strnew1=strnew.replace(/;/g,\",\");\n";
$js .= "               html=html+\"\\\"\"+strnew1+\"\\\"\"+\";\";\n";
$js .= "          }\n";
$js .= "          html=html+\"\\n\";\n";
$js .= "     }\n";
$js .= "     html=html+\"\\n\";\n";
$js .= "     document.forms['frmexcel'].csvBuffer.value=html;\n";
$js .= "     document.forms['frmexcel'].submit();\n";
$js .= "}\n";

$js .= sprintf("function GETLIST(id){\n");
$js .= sprintf("     \$.post('my_ajax/Cad%s_ajax.php',\n", ucfirst($SCRIPT));
$js .= sprintf("     {LIST: '1',ID: ''+last_lin+'',POS: ''+id+''},\n");
$js .= sprintf("     function(result){\n");
$js .= sprintf("          \$.each(result,function(i,field){\n");
$js .= sprintf("               \$('#'+field.id).text(field.value);\n");
$js .= sprintf("               if(field.id == 'last_lin')\n");
$js .= sprintf("               {\n");
$js .= sprintf("                    last_lin = field.value;\$('#ID').val(last_lin);\n");
$js .= sprintf("               }\n");
$js .= sprintf("          }\n");
$js .= sprintf("     );\n");
$js .= sprintf("     },'json'\n");
$js .= sprintf(")\n");
$js .= sprintf("}\n");

$js .= sprintf("function populate(result){\n");
$js .= sprintf("     \$.each(result,function(key,value){\n");
$js .= sprintf("          if(key == 'message')\n");
$js .= sprintf("          {\n");
$js .= sprintf("               var message = value;\n");
$js .= sprintf("               \$('#info').html(\"<h2 align='center'><font color='#ff8080'>\"+message+\"</font></h2>\");\n");
$js .= sprintf("               \$('#dialog-info').dialog('open');\n");
$js .= sprintf("          }\n");
$js .= sprintf("          if(key == 'current')\n");
$js .= sprintf("          {\n");
$js .= sprintf("               current = value;\n               \$('#ID').val(current);\n");
$js .= sprintf("               %s", $pesq);
$sql = sprintf("SELECT campo,popup FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha", $TABLE, $SCRIPT);
$result = mysql_query($sql, $dbctl);
$fim = "";
$pop = "";
$base = "";
if($result == 0)
{
     printf("<h1>(PrepMenu-4)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($result) > 0)
     {
          while($myrow = mysql_fetch_row($result))
          {
               if(substr(strtolower($myrow[0]), 0, 2) == "id")
               {
                    $sel = $myrow[0];
                    if(strstr($sel, "_id") != "")
                    {
                         $tb2 = strstr($sel, "_id");
                         $sel = substr($sel, 0, strlen($sel) - strlen($tb2));
                         $tb1 = ucfirst(substr($sel, 2, strlen($sel) - 2));
                         $tb2 = substr($tb2, 1, strlen($tb2) - 1);
                         $js .= sprintf("                    getOptions_%s($('#%s option:selected').val());\n", strtolower($tb2), strtoupper($tb2));
                         $base = sprintf("     \$(\"select#%s\").change(function(){\n", strtoupper($tb2));
                         $fim .= sprintf("          getOptions_%s(\$(this).val());\n", strtolower($tb2));
                    }
               }
               if($myrow[1] != "")
               {
                    $tb2 = ucfirst(substr($myrow[0], 2, strlen($myrow[0]) - 2));
                    $base = sprintf("     \$(\"select#%s\").change(function(){\n", strtoupper($tb2));
                    $pop .= sprintf("          getPopUP_%s(\$(this).val());\n", strtolower($tb2));
               }
          }
     }
}
$js .= sprintf("          }\n");

$sql = sprintf("SELECT campo,tipo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha", $TABLE, $SCRIPT);
$result = mysql_query($sql, $dbctl);
if($result == 0)
{
     printf("<h1>(PrepMenu-5)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($result) > 0)
     {
          while($myrow = mysql_fetch_row($result))
          {
               if(substr(strtolower($myrow[0]), 0, 2) == "id")
               {
                    $sel = $myrow[0];
                    if(strstr($sel, "_id") != "")
                    {
                         $js .= sprintf("          else if(key == '%s')\n", strtoupper($myrow[0]));
                         $js .= sprintf("          {\n");
                         $js .= sprintf("               %s = value;\n", strtolower($myrow[0]));
                         $js .= sprintf("          }\n");
                    }
               }
               if($myrow[1] == "mediumblob")
               {
                    if(strtoupper($myrow[0]) != 'DOCX')
                    {
                         $js .= sprintf("          else if(key == '%s')\n", strtoupper($myrow[0]));
                         $js .= sprintf("          {\n");
                         $js .= sprintf("               \$('#'+key).attr(\"src\",value);\n");
                         $js .= sprintf("               var spl = value.split(\"=\");\n");
                         $js .= sprintf("               var tmp = \"\";\n");
                         $js .= sprintf("               while((tmp.length+spl[1].length)< 6)\n");
                         $js .= sprintf("               {\n");
                         $js .= sprintf("                    tmp += '0';\n");
                         $js .= sprintf("               }\n");
                         $js .= sprintf("               tmp += spl[1];\n");
                         if(strtoupper($myrow[0]) == 'LOGO')
                         {
                              $js .= sprintf("               \$('#H'+key).attr(\"href\",'ImageUp/logoup.php?DIR=%s_%s&IMAGEM='+tmp);\n", strtolower($TABLE), strtolower($myrow[0]));
                         }
                         else if(strtoupper($myrow[0]) == 'FOTO')
                         {
                              $js .= sprintf("               \$('#H'+key).attr(\"href\",'ImageUp/imageup.php?DIR=%s_%s&IMAGEM='+tmp);\n", strtolower($TABLE), strtolower($myrow[0]));
                         }
                         else if(strtoupper($myrow[0]) == 'DOC')
                         {
                              $js .= sprintf("               \$('#H'+key).attr(\"href\",'doc_up.php?DIR=%s_%s&DOC='+tmp);\n", strtolower($TABLE), strtolower($myrow[0]));
                         }
                         $js .= sprintf("          }\n");
                    }
               }
          }
     }
}
$js .= sprintf("          else if(key == 'SESSION')\n");
$js .= sprintf("          {\n");
$js .= sprintf("               idsessao = value;\n");
$js .= sprintf("               if(idsessao == -1){\n");
$js .= sprintf("                    alert(\"SESSAO INVALIDA, FECHE TODAS AS PAGINAS E FAÇA O LOGIN NOVAMENTE\")\n");
$js .= sprintf("               }\n");
if(isset($_REQUEST['Debug']))
{
     $js .= sprintf("                    $('#DEBUG').val(idsessao);\n");
}
$js .= sprintf("          }\n");
$js .= sprintf("          else if(key == 'CNPJ')\n");
$js .= sprintf("          {\n");
$js .= sprintf("               value = value.replace(/\./g, \"\");\n");
$js .= sprintf("               value = value.replace(/\-/g, \"\");\n");
$js .= sprintf("               value = value.replace(/\_/g, \"\");\n");
$js .= sprintf("               value = value.replace(/\//g, \"\");\n");
$js .= sprintf("               if(value.length == 14) {\n");
$js .= sprintf("                    var res = value.substring(0,2) + \".\" + value.substring(2,5) + \".\" +value.substring(5,8) + \"/\" +value.substring(8,12) + \"-\" +value.substring(12,14);\n");
$js .= sprintf("                    \$('#'+key).val(res);\n");
$js .= sprintf("               } else {\n");
$js .= sprintf("                    $('#'+key).val(value);\n");
$js .= sprintf("               }\n");
$js .= sprintf("          }\n");
$js .= sprintf("          else if(key == 'CPF')\n");
$js .= sprintf("          {\n");
$js .= sprintf("               value = value.replace(/\./g, \"\");\n");
$js .= sprintf("               value = value.replace(/\-/g, \"\");\n");
$js .= sprintf("               value = value.replace(/\_/g, \"\");\n");
$js .= sprintf("               value = value.replace(/\//g, \"\");\n");
$js .= sprintf("               if(value.length == 11) {\n");
$js .= sprintf("                    var res = value.substring(0,3) + \".\" + value.substring(3,6) + \".\" +value.substring(6,9) + \"-\" +value.substring(9,11);\n");
$js .= sprintf("                    \$('#'+key).val(res);\n");
$js .= sprintf("               } else {\n");
$js .= sprintf("                    $('#'+key).val(value);\n");
$js .= sprintf("               }\n");
$js .= sprintf("          }\n");
$js .= sprintf("          else\n");
$js .= sprintf("          {\n");
$js .= sprintf("               if($('#'+key).attr('type') == 'checkbox'){\n");
$js .= sprintf("                    if(value == 'V' || value == 'T' || value == 'S'){\n");
$js .= sprintf("                         \$('#'+key).attr('checked',true);\n");
$js .= sprintf("                    }\n");
$js .= sprintf("                    else {\n");
$js .= sprintf("                       $('#'+key).attr('checked',false);\n");
$js .= sprintf("                    }\n");
$js .= sprintf("               }\n");
$js .= sprintf("               else {\n");
$js .= sprintf("                    var texto = value;\n");
$js .= sprintf("                    var a = 0;\n");
$js .= sprintf("                    while((a = texto.indexOf('+++')) != -1 || (a = texto.indexOf('---')) != -1)\n");
$js .= sprintf("                    {\n");
$js .= sprintf("                         texto = texto.replace('+++','');\n");
$js .= sprintf("                         texto = texto.replace('---','\\n');\n");
$js .= sprintf("                    }\n");
$js .= sprintf("                    $('#'+key).val(texto);\n");
$js .= sprintf("               }\n");
$js .= sprintf("          }\n");
$js .= sprintf("     }\n");
$js .= sprintf("     );\n");
$js .= sprintf("}\n");



$js .= sprintf("function GET(id){\n");
$js .= sprintf("     \$.post('my_ajax/Cad%s_ajax.php',\n", ucfirst($SCRIPT));
$js .= sprintf("     {ID: ''+current+'',POS: ''+id+''},\n");
$js .= sprintf("     function(result){populate(result);}\n");
$js .= sprintf("     ,'json'\n");
$js .= sprintf("     )\n");
$js .= sprintf("     .complete(function() {\n");
if($lnk != '')
{
     $ret = Mount_Link_Js1($db, $TABLE);
     $js .= $ret;
}
$js .= sprintf("     })\n");
$js .= sprintf("     .success(function() {\n");
$js .= sprintf("     })\n");
$js .= sprintf("     .error( function(jqXHR, textStatus, errorThrown) {\n");
$js .= sprintf("          alert(\"error \" + textStatus);\n");
$js .= sprintf("          alert(\"incoming Text \" + jqXHR.responseText);\n");
$js .= sprintf("     });\n");

$js .= sprintf("}\n");

// Se existir campo DOC para efetuar UPLOAD
$sql = sprintf("SELECT campo,tipo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha", $TABLE, $SCRIPT);
$result = mysql_query($sql, $dbctl);
if($result == 0)
{
     printf("<h1>(PrepMenu-5b)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($result) > 0)
     {
          while($myrow = mysql_fetch_row($result))
          {
               if($myrow[1] == "mediumblob")
               {
                    if(strtoupper($myrow[0]) == 'DOCX')
                    {
                         $js .= sprintf("\$(function (){\n");
                         $js .= sprintf("     alert('UPLOAD');\n");
                         $js .= sprintf("     \$('#%s').fileupload({\n", strtoupper($myrow[0]));
                         $js .= sprintf("          dataType: 'json',\n");
                         $js .= sprintf("          url: 'doc_up.php',\n");
                         $js .= sprintf("          done: function (e, data) {\n");
                         $js .= sprintf("               \$each(data.result, function (index, file) { \n");
                         $js .= sprintf("                    \$('<p>').text(file.name).appendTo(document.body);\n");
                         $js .= sprintf("               });\n");
                         $js .= sprintf("          }\n");
                         $js .= sprintf("     });\n");
                         $js .= sprintf("});\n");
                    }
               }
          }
     }
}


$js .= sprintf("function DEL(){\n");
$js .= sprintf("     \$.post('my_ajax/Cad%s_ajax.php',\n", ucfirst($SCRIPT));
$js .= sprintf("     {ID: ''+current+'',POS: 'remove'},\n");
$js .= sprintf("     function(result){populate(result);}\n", $pesq);
$js .= sprintf("     ,'json'\n");
$js .= sprintf("     )\n");
$js .= sprintf("     .complete(function() {\n");
if($lnk != '')
{
     $ret = Mount_Link_Js1($db, $TABLE);
     $js .= $ret;
}
$js .= sprintf("     })\n");
$js .= sprintf("     .success(function() {\n");
$js .= sprintf("     })\n");
$js .= sprintf("     .error( function(jqXHR, textStatus, errorThrown) {\n");
$js .= sprintf("          alert(\"error \" + textStatus);\n");
$js .= sprintf("          alert(\"incoming Text \" + jqXHR.responseText);\n");
$js .= sprintf("     });\n");

$js .= sprintf("}\n");

$js .= sprintf("function SAVE(){\n");
$js .= sprintf("     \$('#save').val(current);\n");
$js .= sprintf("     var url = \$('#frmdata').serialize();\n");
if(isset($_REQUEST['Debug']))
{
     $js .= sprintf("     $('#DEBUG').val('my_ajax/Cad%s_ajax.php?'+url);\n", ucfirst($SCRIPT));
}

$js .= sprintf("     var r = confirm('Confirma a Atualização/Inclusão do Registro?');\n");
$js .= sprintf("     if(r == true)\n");
$js .= sprintf("     {\n");
$js .= sprintf("          \$.post('my_ajax/Cad%s_ajax.php',\n", ucfirst($SCRIPT));
$js .= sprintf("          url,\n");
$js .= sprintf("          function(result){populate(result);}\n", $pesq);
$js .= sprintf("     ,'json'\n");
$js .= sprintf("     )\n");
$js .= sprintf("     .complete(function() {\n");
if($lnk != '')
{
     $ret = Mount_Link_Js1($db, $TABLE);
     $js .= $ret;
}
$js .= sprintf("     })\n");
$js .= sprintf("     .success(function() {\n");
$js .= sprintf("     })\n");
$js .= sprintf("     .error( function(jqXHR, textStatus, errorThrown) {\n");
$js .= sprintf("          alert(\"error \" + textStatus);\n");
$js .= sprintf("          alert(\"incoming Text \" + jqXHR.responseText);\n");
$js .= sprintf("     });\n");

$js .= sprintf("     }\n");
$js .= sprintf("     Disable();\n");
$js .= sprintf("     edit = 0;\n");
$js .= sprintf("     if(current == 0)\n");
$js .= sprintf("     {\n");
$js .= sprintf("          current = old;\n");
$js .= sprintf("          GET('');\n");
$js .= sprintf("     }\n");
$js .= sprintf("}\n");

$js .= sprintf("$(function(){\n");
$js .= sprintf("    \$( '#tabs' ).tabs();\n");
$js .= sprintf("})\n");

$js .= sprintf("function Disable(){\n");
$js .= sprintf("     var \$inputs = \$('#frmdata :input');\n");
$js .= sprintf("     \$inputs.each(function() {\n");
$js .= sprintf("          \$(this).attr('disabled',true);\n");
$js .= sprintf("     })\n");
if($lnk != '')
{
     $ret = Mount_Link_Js3($db, $TABLE);
     $js .= $ret;
}
$js .= sprintf("}\n");

$js .= sprintf("function Enable(){\n");
$js .= sprintf("     var \$inputs = \$('#frmdata :input');\n");
$js .= sprintf("     \$inputs.each(function() {\n");
$js .= sprintf("          \$(this).attr('disabled',false);\n");
$js .= sprintf("     })\n");
if($lnk != '')
{
     $ret = Mount_Link_Js2($db, $TABLE);
     $js .= $ret;
}
$js .= sprintf("}\n");

$js .= sprintf("function Limpa() { \$('#ID').val('');\$('#PESQUISA').val('');var \$inputs = \$('#frmdata :input'); \$inputs.each(function() {\$(this).val('')})};\n");
$js .= sprintf("\$(function() {\n");
$js .= sprintf("   \$( '#PESQUISA' ).autocomplete({\n");
$js .= sprintf("        source: 'my_ajax/Cad%s_ajax.php?type=PESQUISA',\n", ucfirst($SCRIPT));
$js .= sprintf("        minLength: 2,\n");
$js .= sprintf("        select: function (event, ui) {current = ui.item.id; GET(''); }\n");
$js .= sprintf("   });\n");
$js .= sprintf("   $('#PESQUISA').click(function() { $(this).val('');});\n");
$js .= sprintf("})\n");

$sql = sprintf("SELECT campo,pesquisa,popup FROM Page_mount WHERE Tabela='%s' AND script='%s'", $TABLE, $SCRIPT);
$result = mysql_query($sql, $dbctl);
if($result == 0)
{
     printf("<h1>(PrepMenu-6)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($result) > 0)
     {
          while($myrow = mysql_fetch_row($result))
          {
               if(substr(strtolower($myrow[0]), 0, 2) == "id")
               {
                    if(strstr($myrow[0], "_id") != "")
                    {
                         $sel = $myrow[0];
                         $tb2 = strstr($sel, "_id");
                         $sel = substr($sel, 0, strlen($sel) - strlen($tb2));
                         $tb1 = ucfirst(substr($sel, 2, strlen($sel) - 2));
                         $tb2 = substr($tb2, 1, strlen($tb2) - 1);
                         $js .= sprintf("function getOptions_%s(id)\n", strtolower($tb2));
                         $js .= sprintf("{\n");
                         $js .= sprintf("     if(id != 0) {\n");
                         $js .= sprintf("          popupid = id;\n");
                         $js .= sprintf("          \$.post(\"my_ajax/Cad%s_ajax.php\", {\n", ucfirst($SCRIPT));
                         $js .= sprintf("               %s: id\n", strtoupper($tb2));
                         $js .= sprintf("          }, function(j){\n");
                         $js .= sprintf("               var options = '';\n");
                         $js .= sprintf("               for (var i = 0; i < j.length; i++) {\n");
                         $js .= sprintf("                    if(j[i].optionValue == %s) {\n", strtolower($myrow[0]));
                         $js .= sprintf("                         options += '<option value=\"' + j[i].optionValue + '\" selected=selected>' + j[i].optionDisplay + '</option>';\n");
                         $js .= sprintf("                    }\n");
                         $js .= sprintf("                    else {\n");
                         $js .= sprintf("                         options += '<option value=\"' + j[i].optionValue + '\">' + j[i].optionDisplay + '</option>';\n");
                         $js .= sprintf("                    }\n");
                         $js .= sprintf("               }\n");
                         $js .= sprintf("               \$(\"select#%s\").html(options);\n", strtoupper($myrow[0]));
                         $js .= sprintf("          },'json')\n");
                         if($pop != "")
                         {
                              $js .= sprintf("          .complete(function() { getPopUP_%s(popupid);});\n", strtolower($tb2));
                         }
                         $js .= sprintf("     }\n");
                         $js .= sprintf("}\n");
                    }
                    if($myrow[1] == "1")
                    {
                         $js .= sprintf("\$(function() {\n");
                         $js .= sprintf("   \$( '#PESQ_%s' ).autocomplete({\n", strtoupper($myrow[0]));
                         $js .= sprintf("        source: 'my_ajax/Cad%s_ajax.php?type=PESQ_%s',\n", ucfirst($SCRIPT), strtoupper($myrow[0]));
                         $js .= sprintf("        minLength: 2,\n");
                         $js .= sprintf("        select: function (event, ui) {current = ui.item.id; GET(''); }\n");
                         $js .= sprintf("   });\n");
                         $js .= sprintf("   $('#PESQ_%s').click(function() { $(this).val('');});\n", strtoupper($myrow[0]));
                         $js .= sprintf("})\n");
                    }
                    if($myrow[2] != "")
                    {
                         $js .= sprintf("function getPopUP_%s(id)\n", strtolower($tb2));
                         $js .= sprintf("{\n");
                         $js .= sprintf("     if(id != 0) {\n");
                         $js .= sprintf("          \$.post(\"my_ajax/Cad%s_ajax.php\", {\n", ucfirst($SCRIPT));
                         $js .= sprintf("               %s: id,\n", strtoupper($tb2));
                         $js .= sprintf("               POPUP: '%s'\n", $myrow[2]);
                         $js .= sprintf("          }, function(j){\n");
                         $js .= sprintf("               for (var i = 0; i < j.length; i++) {\n");
                         $js .= sprintf("                    if(j[i].popupValue == 'POPUP') {\n");
                         $js .= sprintf("                         if(j[i].popupDisplay != ''){\n");
                         $js .= sprintf("                         alert(j[i].popupDisplay);\n");
                         $js .= sprintf("                         break;}\n");
                         $js .= sprintf("                    }\n");
                         $js .= sprintf("               }\n");
                         $js .= sprintf("          },'json')\n");
                         $js .= sprintf("     }\n");
                         $js .= sprintf("}\n");
                    }
               }
          }
     }
}
if($base != "")
{
     $js .= "$(function(){\n";
     $js .= $base;
     if($fim != "")
     {
          $js .= $fim;
     }
     $js .= "     })\n";
     $js .= "});\n";
}
$js .= "$(function(){\n";
$out .= sprintf("<script type=\"text/javascript\" src=\"my_js/Cad%s.js\"></script>\n", ucfirst($SCRIPT));
$out .= sprintf("</head>\n");
$out .= sprintf("<body>\n");

/*
 * Preparando toolbar
 */
$out .= "<?php \$filename = \"./includes/Menu.inc.php\";\n";
$out .= "if(file_exists(\$filename))\n";
$out .= "{\n";
$out .= "     include \"./includes/Menu.inc.php\";?>\n";
$out .= "     <br />\n";
$out .= "     <br />\n";
$out .= "     <div class='titulo'></div>\n";
$out .= "<?php } ?>\n";
$out .= "     <span id='toolbar' class='ui-widget-header ui-corner-all'>\n";
$out .= "          <button id='list'>Lista</button>\n";
$out .= "          <button id='beginning'>Inicio</button>\n";
$out .= "          <button id='rewind'>Anterior</button>\n";
$out .= "          <button id='forward'>Próximo</button>\n";
$out .= "          <button id='end'>Último</button>\n";
$out .= getDirEdit($db, $TABLE);
$out .= "          <button id='novo'>Novo</button>\n";
$out .= "          <button id='editar'>Editar</button>\n";
$out .= "          <button id='excluir'>Excluir</button>\n";
$out .= "          <button id='salvar'>Salvar</button>\n";
$out .= "<?php } ?>\n";
if(isset($_REQUEST['AGENDA']))
{
     $out .= "          <button id='agenda'>Agenda</button>\n";
}
$sql = "SELECT menu,titulo,url FROM Menu_mount WHERE menu LIKE 'button|%' ORDER BY menu,titulo";
$result = mysql_query($sql, $dbctl);
if($result == 0)
{
     printf("<h1>(function-7)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($result) > 0)
     {
          while($myrow = mysql_fetch_row($result))
          {
               $VAR = explode("|", $myrow[0]);
               if(strtoupper($VAR[1]) == strtoupper($TABLE))
               {
                    $out .= sprintf("          &nbsp;&nbsp;<button id='%s'>%s</button>\n", strtolower($VAR[2]), ucwords($myrow[1]));
                    $js .= sprintf("     \$('#%s',\$('#toolbar')).button({\n", strtolower($VAR[2]));
                    if($VAR[6] == "1")
                    {
                         $js .= "          text: true,\n";
                    }
                    else
                    {
                         $js .= "          text: false,\n";
                    }
                    $js .= "          icons: {\n";
                    $js .= sprintf("               primary: \"%s\"\n", $VAR[3]);
                    $js .= "          }\n";
                    $js .= "     });\n";

                    $js .= sprintf("     \$('#%s',\$('#toolbar')).click(function() {\n", strtolower($VAR[2]));
                    if(strstr($myrow[2], "?") != "")
                    {
                         $js .= sprintf("          var url = '%s=' + current;\n", $myrow[2]);
                    }
                    else
                    {
                         $js .= sprintf("          var url = '%s?ID=' + current;\n", $myrow[2]);
                    }
                    if($VAR[4] == "")
                    {
                         $VAR[4] = '700';
                    }
                    if($VAR[5] == "")
                    {
                         $VAR[5] = '950';
                    }
                    $js .= sprintf("          window.open(url, '%s', 'width=%s, height=%s, scrollbars=yes, location=no');\n", $VAR[2], $VAR[4], $VAR[5]);
                    $js .= "     });\n";
               }
          }
     }
}
$out .= "     </span>\n";
$out .= "     <br />\n";
if(isset($_REQUEST['LOGOTIPO']))
{
     $out .= "     <div class='titulo'><img src='img/logo1.png'/>&nbsp;&nbsp;" . $TITULO . "</div>\n";
}
else
{
     $out .= "     <div class='titulo'>" . $TITULO . "</div>\n";
}
$out .= "     <form id='frmpesq' action='#' class='frmpesq'>\n";
$out .= "          <fieldset>\n";
$out .= "               <legend></legend>\n";
$out .= "               <span class='lbl_id'>Reg.:</span>\n";
$out .= "               <span class='entry_pesq'><input type='text' id='ID' name='ID' size='8' disabled='disabled' class='text ui-widget-content ui-corner-all'/></span>\n";
$out .= "               <span class='lbl_pesq'>Pesquisa:</span>\n";
$out .= "               <span class='entry_pesq'><input type='text' id='PESQUISA' name='PESQUISA' size='80' class='text ui-widget-content ui-corner-all'/></span>\n";
$out .= "          </fieldset>\n";
$out .= "     </form>\n";
?>
